package com.itheima.d3_annotation2;

import org.junit.Test;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 解析注解
 */

public class AnnotationDemo1 {
    /**
     * 解析类上的注解
     */
    @Test
    public void parseClass()throws  Exception{
        //1.获取类的对象
        Class<Demo> c = Demo.class;
        //2.判断是否有对应的注解
        if(c.isAnnotationPresent(MyTest.class)){
            //3获取注解对象
            MyTest myTest = c.getDeclaredAnnotation(MyTest.class);
            //4.获取注解中的属性值
            String value = myTest.value();
            double height = myTest.height();
            String[] address = myTest.address();
            //5.打印注解属性值
            System.out.println("value:"+value);
            System.out.println("height:"+height);
            System.out.println("address:"+ Arrays.toString( address));
        }
    }

    //解析方法上的注解
    @Test
    public void parseMethod()throws Exception{
        //1.拿到类的字节码文件
        Class<Demo> c = Demo.class;
        //2.用类的字节码对象 得到方法对象
        Method go = c.getDeclaredMethod("go");
        //3.判断方法上是否有MyTest注解
        if(go.isAnnotationPresent(MyTest.class)){
            //4.方法上有MyTest注解 那么拿到注解的对象  myTest
            MyTest myTest = go.getDeclaredAnnotation(MyTest.class);
            //5.通过注解的对象获取注解中属性的值
            String value = myTest.value();
            double height = myTest.height();
            String[] address = myTest.address();
            System.out.println("value:"+value);
            System.out.println("height:"+height);
            System.out.println("address:"+ Arrays.toString(address));
        }


    }
}
